import pytest
from copy import deepcopy
from lly.algorithm.sort.selection import Selection
from lly.algorithm.sort.base import Sort
from lly.algorithm.sort.insertion import Insertion
from lly.algorithm.sort.mergation import Mergation
from lly.algorithm.sort.quick import Quick
from lly.algorithm.sort.heap import Heap


@pytest.fixture(params=range(5+3), scope='function')
def data(request):
    if request.param == 0:
        yield list(range(0,400))
    elif request.param== 1:
        yield [6]*400
    elif request.param== 2:
        yield list(range(400,0,-1))
    else:
        #  data = Sort.generate_data(40, hight=100)
        data = Sort.generate_data(20000, hight=200)
        yield data


def test_sort(data):
    lt = not True
    answer_data = sorted(deepcopy(data), reverse=not lt)
    sorts = []
    sorts.append(Quick(lt))
    #  sorts.append(Insertion(lt))
    sorts.append(Mergation(lt))
    #  sorts.append(Selection(lt))
    result = {}
    for item in sorts:
        temp_data = deepcopy(data)
        result[item.name()] = item.statistics(temp_data)
        #  print()
        #  print(answer_data)
        #  print(item.name(), temp_data)
        for i in range(len(answer_data)):
            assert answer_data[i] == temp_data[i]
        assert item.is_satisfy(temp_data)
    items = sorted(result.items(), key=lambda item: item[1])
    print(f"结果")
    for name, time in items:
        print(f"{name}:{time}")


def test_pq(data):
    lt = True
    heap = Heap(lt)
    exec_time=heap.statistics(data)
    print(f"{heap.name()}:{exec_time}")
    temp = deepcopy(data)
    answer_data = sorted(temp, reverse=not lt)
    for i in range(len(data)):
        value = heap.dequeue()
        assert answer_data[i] == value
